test_that("is_country_date", {
    expect_true(is_country_date("SWE 1960-12-31"))

    # We're not currently checking if the date is valid
    expect_true(is_country_date("SVN 2002-99-99"))

    expect_equal(is_country_date(c("AFG 1901-01-01", NA_character_)), c(T, F))

    expect_false(is_country_date("historical_1_v1"))
    expect_false(is_country_date("BRN 66-12-31"))
    expect_false(is_country_date("pre 1945-10-11"))
    expect_false(is_country_date("AFG1900-01-01"))
    expect_false(is_country_date("AAAA 1904-02-13"))
    expect_false(is_country_date("abc 1900-01-01"))
    expect_false(is_country_date("AFG 1999-100-99"))
    expect_false(is_country_date(NA))
    expect_false(is_country_date(1))

    expect_equal(is_country_date(NULL), logical(0))
})

test_that("is_vignette", {
    expect_equal(is_vignette(c("contemporary_1_v1", "pilot_3_v3",
                               "historical_2_v1", "BRA 1871-12-01")),
                 c(T, T, T, F))
    expect_equal(is_vignette(c("contemporary_2_v2", NA, "MDG 1900")), c(T, F, F))
})

test_that("get_date", {
    expect_equal(get_date(c("UUU 1900-01-01", "ARG 2016-11-24")),
                 as.Date(c("1900-01-01", "2016-11-24")))
    expect_error(get_date("historical_3_v3"))
    expect_error(get_date(c(NA, "USA 1879-11-10")))
    expect_error(get_date("NAM 1999-99-99"))
})

test_that("get_text_id", {
    expect_equal(get_text_id(c("ARG 1906-06-01", "EDD 1789-12-31")), c("ARG", "EDD"))
    expect_error(get_text_id("pilot_1_v1"))
    expect_error(get_text_id(c(NA, "ARG 1906-06-01")))
    expect_error(get_text_id(c("MMMM 1888-08-08")))
})

test_that("sort.country_date", {
    input <- c("AFG 1800-01-01", "pilot_2_v1", "pilot_1_v1",
              "contemporary_2_v2", "AFG 1776-12-31", "contemporary_2_v1")
    output <- c("AFG 1776-12-31", "AFG 1800-01-01", "contemporary_2_v1",
               "contemporary_2_v2", "pilot_1_v1", "pilot_2_v1")
    expect_equal(sort_text_id(input), output)

    input <- c("AFG 1900-01-01", "USA 1890-12-31", "USA 1891-12-31", "USA 1891-02-03")
    output <- c("AFG 1900-01-01", "USA 1890-12-31", "USA 1891-02-03", "USA 1891-12-31")
    expect_equal(sort_text_id(input), output)

    expect_error(sort_text_id(c("USA 1900-01-01", "USA 1888-08-04", NA)))
})

test_that("to_year", {
    expect_equal(to_year(as.Date("1900-01-01")), 1900L)
    expect_equal(to_year(as.Date("2016-11-24")), 2016L)
    expect_equal(to_year(as.Date(c(NA, "1995-01-01"))), c(NA_integer_, 1995L))
    expect_equal(to_year(as.Date("16-12-14")), 16L) # This is weird, but it's a valid date

    expect_error(to_year(2016))
    # expect_error(to_year("2016-11-24"))
    expect_error(to_year(2016-11-24))
    expect_error(to_year(NA))
})

test_that("create_idx", {
    x <- c(1900, 1901, 1903, 1904, 1906, 1907)
    expect_equal(create_idx(x), c(1, 1, 2, 2, 3, 3))

    # Hope your input is sorted properly
    x <- c(1999, 1998, 1997)
    expect_equal(create_idx(x), c(1, 1, 1))

    x <- as.Date(c("1900-01-01", "1901-11-08", "1901-12-31", "1904-01-01", "1905-01-01"))
    expect_equal(create_idx(x), c(1, 2, 2, 3, 3))

    expect_error(create_idx(c(1900, NA)))
    expect_error(create_idx(as.Date(c("1900-01-01", NA))))
    expect_error(create_idx("1900-01-01"))
    expect_error(create_idx(c(90, 12)))
})
